package com.wzd.imservice.controller;

import com.wzd.baseservice.exceptionHandler.BaseException;
import com.wzd.commonutils.JwtUtils;
import com.wzd.commonutils.R;
import com.wzd.imservice.client.EduServiceClient;
import com.wzd.imservice.client.OrderClient;
import com.wzd.imservice.entity.GroupList;
import com.wzd.imservice.entity.Mine;
import com.wzd.imservice.service.GroupService;
import com.wzd.imservice.service.MineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * @ProjectName: e_learning
 * @Package: com.wzd.imservice.controller
 * @ClassName: GroupController
 * @Author: lofxve
 * @Description:
 * @Date: 2021/4/16 17:01
 * @Version: 1.0
 */
@RestController
@Api(tags = "班级管理")
@RequestMapping("/eduim/group")
public class GroupController {
    @Autowired
    private GroupService groupService;

    @Autowired
    private MineService mineService;

    @Autowired
    private OrderClient orderClient;

    @Autowired
    private EduServiceClient eduServiceClient;

    @ApiOperation("新增班级")
    @PostMapping("saveGroup")
    public R saveGroup(@RequestBody GroupList groupList) {
        if (groupList.getMaster() != null) {
            Mine mineById = mineService.getMineById(groupList.getMaster());
            if (mineById == null) {
                throw new BaseException(20001, "管理员用户不存在");
            }
            ArrayList<Mine> mines = new ArrayList<>();
            // 群主
            mineById.setRole("master");
            mines.add(mineById);
            groupList.setUserList(mines);
            return groupService.saveGroup(groupList);
        } else {
            return R.error();
        }
    }

    @ApiOperation("删除班级")
    @GetMapping("deleteGroup/{id}")
    public R deleteGroup(@PathVariable("id") String id) {
        boolean flag = groupService.deleteGroup(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation("根据id获取班级")
    @GetMapping("getGroupById")
    public R getGroupById(@RequestParam("id") String id) {
        GroupList mine = groupService.getGroupById(id);
        if (mine != null) {
            return R.ok().data("group", mine);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("为班级新增学生")
    @PostMapping("addUserToGroup")
    public R addUserToGroup(@RequestParam("groupId") String groupId, @RequestParam("userId") String userId) {
        Mine mineById = mineService.getMineById(userId);
        if (mineById == null) {
            throw new BaseException(20001, "用户不存在");
        }

        GroupList groupById = groupService.getGroupById(groupId);
        if (groupById == null) {
            throw new BaseException(20001, "班级不存在");
        }
        R save = groupService.addUserToGroup(mineById, groupById);
        return save;
    }

    @ApiOperation("从班级中删除学生")
    @PostMapping("deleteUserFromGroup")
    public R deleteUserFromGroup(@RequestParam("groupId") String groupId, @RequestParam("userId") String userId) {
        Mine mineById = mineService.getMineById(userId);
        if (mineById == null) {
            throw new BaseException(20001, "用户不存在");
        }
        GroupList groupById = groupService.getGroupById(groupId);
        if (groupById == null) {
            throw new BaseException(20001, "班级不存在");
        }
        if (userId.equals(groupById.getMaster())) {
            throw new BaseException(20001, "不能删除管理员");
        }
        R save = groupService.deleteUserFromGroup(mineById, groupById);
        return save;
    }

    @ApiOperation("根据用户id查询用户会话列表")
    @GetMapping("getGroupsByUserId/{id}")
    public R getGroupsByUserId(@PathVariable("id") String courseId, HttpServletRequest request) {
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberIdByJwtToken)) {
            return R.error().code(28004).message("请登录");
        }
        // 是否购买
        boolean buyCourse = orderClient.isBuyCourse(memberIdByJwtToken, courseId);
        // 该课程是否是公开课
        boolean publicByCourseId = eduServiceClient.isPublicByCourseId(courseId);

        // 如果是公开课，或者已经购买，则将用户添加进班级
        if (buyCourse || publicByCourseId) {
            this.addUserToGroup(courseId, memberIdByJwtToken);
        }
        Mine mineById = mineService.getMineById(memberIdByJwtToken);
        if (mineById == null) {
            throw new BaseException(20001, "用户不存在");
        }
        R save = groupService.getGroupsByUserId(mineById);
        return save;
    }
}
